import { AES, enc, mode, pad } from 'crypto-js'

const DEFAULT_AES_KEY = enc.Utf8.parse('3819242ae5d544a3')
const DEFAULT_AES_IV = enc.Utf8.parse('a3afd79bbda54ac4')

/**
 * AES 加密
 * @param str 要加密的字符串
 * @param AESKEY AES 加密密钥
 * @param AESIV AES 加密 iv
 * @returns 加密后的字符串
 */
export const encryptAES = (str: string, AESKEY = DEFAULT_AES_KEY, AESIV = DEFAULT_AES_IV) => {
  const encStr = AES.encrypt(enc.Utf8.parse(str), AESKEY, {
    iv: AESIV,
    mode: mode.CBC,
    padding: pad.ZeroPadding,
  })
  return encStr.toString()
}

/**
 * AES 解密
 * @param str 要解密的字符串
 * @param AESKEY AES 解密密钥
 * @param AESIV AES 解密 iv
 * @returns 解密后的字符串
 */
export const decryptAES = (str: string, AESKEY = DEFAULT_AES_KEY, AESIV = DEFAULT_AES_IV) => {
  const decStr = AES.decrypt(str, AESKEY, {
    iv: AESIV,
    mode: mode.CBC,
    padding: pad.ZeroPadding,
  })
  return enc.Utf8.stringify(decStr)
}
